#include"CircleQueue.h"

void Init_CirQueue(CirQueue* pcq)
{
	assert(pcq);

	pcq->arr = (ELEMTYPE*)malloc(sizeof(ELEMTYPE) * MAXSIZE);

	pcq->front = pcq->rear = 0;
}

bool Push(CirQueue* pcq, ELEMTYPE val)
{
	assert(pcq);

	if (Full(pcq))
		return false;

	pcq->arr[pcq->rear] = val;
	pcq->rear = (pcq->rear + 1) % MAXSIZE;

	return true;
}

bool Pop(CirQueue* pcq)
{
	assert(pcq);

	if (Empty(pcq))
		return false;

	pcq->front = (pcq->front + 1) % MAXSIZE;

	return true;
}

ELEMTYPE Front(CirQueue* pcq)
{
	assert(pcq);

	if (Empty(pcq))
		exit(EXIT_FAILURE);

	return pcq->arr[pcq->front];
}

int Size(CirQueue* pcq)
{
	assert(pcq);

	return (pcq->rear - pcq->front + MAXSIZE) % MAXSIZE;
}

bool Empty(CirQueue* pcq)
{
	assert(pcq);

	return pcq->front == pcq->rear;
}

bool Full(CirQueue* pcq)
{
	assert(pcq);

	return ((pcq->rear + 1) % MAXSIZE) == pcq->front;
}

void Clear(CirQueue* pcq)
{
	assert(pcq);

	free(pcq->arr);
	pcq->arr = NULL;
	pcq->front = pcq->rear = 0;
}

void Destroy(CirQueue* pcq)
{
	assert(pcq);

	Clear(pcq);
}

void Show(CirQueue* pcq)
{
	assert(pcq);
	
	for (int i = pcq->front; i != pcq->rear; i = (i + 1) % MAXSIZE)
		printf("%d -> ", pcq->arr[i]);

	printf("NULL\n");
}

int main()
{
	CirQueue cq;

	Init_CirQueue(&cq);
	printf("%d\n", Size(&cq));

	Push(&cq, 1);
	Push(&cq, 2);
	Push(&cq, 3);
	Push(&cq, 4);
	Push(&cq, 5);
	Push(&cq, 6);
	Push(&cq, 7);
	Push(&cq, 8);
	Push(&cq, 9);
	printf("%d\n", cq.rear);


	Show(&cq);
	printf("%d\n", Size(&cq));
	printf("%d\n", Front(&cq));

	Pop(&cq);
	Pop(&cq);
	Push(&cq, 10);
	Show(&cq);
	printf("%d\n", Size(&cq));
	printf("%d\n", Front(&cq));
	printf("%d\n", cq.rear);

	Destroy(&cq);
	printf("%d\n", Size(&cq));

	return 0;
}